/**
 * 张强 on 20170729
 * 年检支付后，用户地址列表：里面地址组件
 */
import React, { Component } from 'react'
import style from './index.scss'
import {Toast,SwipeAction,Modal} from 'antd-mobile'
import getAddAddressService from '../../services/addAddressListService';
const alert = Modal.alert;

export default class Address extends Component {
	constructor(props) {
		super(props);
		this.state = {
			selectIndex: this.props.fn,
		}
	}
	/* 
	 * 事件
	 */
	getType(i,id,opt) {
		this.props.onType(i,id,opt)
	}

	editAddress(url) {
		sessionStorage.setItem('addressId_i', this.props.addressId)  //编辑地址用
		this.context.router.push(url);
	}

	deleteAddress(index,id,name){
		alert('', '确认要删除收件人为'+name+'的收件地址？', [
			{ text: '取消', onPress: () => console.log('cancel') },
			{
				text: '确定',
				onPress: () =>{
					let postData={
						id: id
					}
					Toast.hide(); 
					Toast.loading("", 30, () => Toast.info("网络错误", 2));
					getAddAddressService.deleteAddress(postData).then(resultData => {
						Toast.hide();
						if (resultData.code == 1000) {
							if(index == 0){
								localStorage.setItem('addressList_action',-1)
								this.props.onClick(-1)
							}else{
								setTimeout(()=>{
									this.props.onClick()
								},0)
							}	
						} else {
							Toast.info(resultData.msg);
						}
					});
				}
			},
		])
	}

	componentWillUnmount() {

	}
	/**
	 * 组件
	 */
	//index:序列； addr地址； name姓名； phone电话； fn当前选中第几条；ifGray：true 表示 置灰，false 表示不置灰，可选
	render() {
		let { index, id, addr, contactName, cellNum, fn, countyName ,street} = this.props;

		return (
			<div className={style.curreentList}>
				<div className={style.addressList+' '+'border_bottom_1'} onClick={(e) => {this.getType(index,id,'choice');}}>
					<div className={style.contacts}>
						<img className={style.map} src="./images/maps.png" alt="" />
						<span>{contactName}</span><span>{cellNum}</span>
					</div>
					<div className={style.loction}>{addr+countyName+street}</div>
				</div>
				<div className={style.featrue}>
					<div className={style.default} onClick={(e) => {this.getType(index,id,'default');}}>
						{
							index == fn?<img className={style.edit} src="./images/yes_choice.png" alt=""/>:<img className={style.edit} src="./images/no_choice.png" alt=""/>
						}
						{
							index == fn?<span className="font-28">默认地址</span>:<span className="font-28">设为默认</span>
						}
					</div>

					<div className={style.repeat}>
						<div className={style.press} onClick={(e)=>{this.deleteAddress(index,id,contactName);e.stopPropagation();}}>
							<img src="./images/dustbin.png" alt="" className={style.dustbin}/><span style={{marginLeft: '.5rem'}}>删除</span>
						</div>
						<div className={style.press} onClick={(e)=>{this.editAddress(`addressEdit/${id}`);e.stopPropagation();}}>
							<img src="./images/edit.png" alt="" className={style.edit}/><span>编辑</span>
						</div>
					</div>

				</div>
				<div style={{height: '.2rem',background:'#f2f2f2'}}></div>
			</div>

		)
	}
}

Address.contextTypes = {
	router: React.PropTypes.object.isRequired
};

Address.defaultProps = {

};

